﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Experiments
{
    partial class Program
    {
        static void ConvertParshantIndFutTickfiles()
        {
            // Myformat: 19970220:10:42:00,394.48

            var years = new[] {"2006", "2007", "2008", "2010"};
            foreach (var year in years)
            {
                string stocksDirOrig = @"H:\StockData\1Parshant-Global-data\1-minute-indian-futures-unzip";
                string stocksDirNew = @"H:\StockData\1Parshant-Global-data\1-minute-indian-futures-myformat";

                stocksDirOrig = Path.Combine(stocksDirOrig, year);
                stocksDirNew = Path.Combine(stocksDirNew, year);

                if (!Directory.Exists(stocksDirNew))
                    Directory.CreateDirectory(stocksDirNew);

                IEnumerable<string> stockPaths = Directory.EnumerateFiles(stocksDirOrig, "*");
                    //, SearchOption.AllDirectories);

                var stocksDict = new Dictionary<string, List<string>>();

                foreach (string filePath in stockPaths)
                {
                    string dateFileName = Path.GetFileName(filePath);
                    var date = dateFileName.Remove(dateFileName.IndexOf('.'));
                    date = date.Insert(4, "-").Insert(7, "-");
                    var lastTradeTime = DateTime.Parse(date); // Remove extension

                    using (var sr = new StreamReader(filePath))
                    {
                        string tick = null;
                        while ((tick = sr.ReadLine()) != null)
                        {
                            try
                            {

                                string[] tickRecord = tick.Split('|');

                                if (!stocksDict.ContainsKey(tickRecord[0]))
                                {
                                    stocksDict.Add(tickRecord[0], new List<string>(500));
                                }

                                var dateTime = lastTradeTime + TimeSpan.Parse(tickRecord[2]);

                                double ltp = double.Parse(tickRecord[3]);
                                ltp = Math.Round(ltp, 2);
                                string str = dateTime.ToString("yyyyMMdd:HH:mm:ss") + "," + ltp;

                                stocksDict[tickRecord[0]].Add(str);

                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }

                        }
                        ; // while (!sr.EndOfStream) ;
                    }

                    // Now write back the files
                    foreach (var kv in stocksDict)
                    {
                        string newFile = Path.Combine(stocksDirNew, kv.Key + ".csv");
                        if (!File.Exists(newFile))
                        {
                            var fs = File.Create(newFile);
                            fs.Close();
                            fs.Dispose();
                        }

                        using (var fs = new FileStream(newFile, FileMode.Append, FileAccess.Write))
                        using (var sw = new StreamWriter(fs))
                            kv.Value.ForEach(x => sw.WriteLine(x));

                        kv.Value.Clear();
                    }

                    GC.Collect();
                }
            }
        }

        static void ShortenTickFiles()
        {
            string stocksDirOrig = @"H:\StockData\1Parshant-Global-data\1-minute-indices-myformat";
            string stocksDirNew = @"H:\StockData\1Parshant-Global-data\1-minute-indices-short";

            if (!Directory.Exists(stocksDirNew))
                Directory.CreateDirectory(stocksDirNew);

            IEnumerable<string> stockPaths = Directory.EnumerateFiles(stocksDirOrig, "*");//, SearchOption.AllDirectories);

            string symbol1 = "";

            foreach (string filePath in stockPaths)
            {
                string stockName = Path.GetFileName(filePath);
                string newFile = Path.Combine(stocksDirNew, stockName);
                var fs = File.Create(newFile);
                fs.Close();
                fs.Dispose();

                int tickCnt = 0;
                //File.OpenRead()
                
                using (var sr = new StreamReader(filePath))
                using (var sw = new StreamWriter(newFile))
                {
                    do
                    {
                        try
                        {
                           
                            sw.WriteLine(sr.ReadLine());
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                    } while ( tickCnt++ < 100000);
                }
            }
        }

        static void ConvertParshantFilesToMyTickfileformat()
        {
            string stocksDirOrig = @"H:\StockData\1Parshant-Global-data\1-minute-indices-unzip\BigFiles";
            string stocksDirNew = @"H:\StockData\1Parshant-Global-data\1-minute-indices-myformat";

            if (!Directory.Exists(stocksDirNew))
                Directory.CreateDirectory(stocksDirNew);

            IEnumerable<string> stockPaths = Directory.EnumerateFiles(stocksDirOrig, "*");//, SearchOption.AllDirectories);

            string symbol1 = "";

            foreach (string filePath in stockPaths)
            {
                string stockName = Path.GetFileName(filePath);
                string newFile = Path.Combine(stocksDirNew, stockName);
                var fs = File.Create(newFile);
                fs.Close();
                fs.Dispose();

                using (var sr = new StreamReader(filePath))
                using (var sw = new StreamWriter(newFile))
                {
                    string tick = sr.ReadLine();
                    do
                    {
                        try
                        {
                            tick = sr.ReadLine();

                            string[] tickRecord = tick.Split(',');

                            string tempStr = tickRecord[0].Substring(3, 3);
                            tickRecord[0] = tickRecord[0].Remove(3, 3);
                            tempStr = tickRecord[0].Insert(0, tempStr);

                            DateTime lastTradeTime = DateTime.Parse(tempStr);
                            tempStr = tickRecord[1].Insert(2, ":");

                            //int time = int.Parse(tempStr);

                            lastTradeTime += TimeSpan.Parse(tempStr);

                            double ltp = double.Parse(tickRecord[4]);
                            string str = lastTradeTime.ToString("yyyyMMdd:HH:mm:ss") + "," + ltp;
                            sw.WriteLine(str);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                    } while (!sr.EndOfStream);
                }
            }
        }

        static void ConvertToMyTickfileformat()
        {
            int[] arr = new int[10000];
            //return;


            string fileName = @"H:\StockData\1Parshant-Global-data\1-minute-indices-unzip";
            string tickFileName = @"D:\StockData\NIFTY_ticks.txt";
            //FileStream fs = new FileStream(tickFileName, FileMode.Create, FileAccess.Write);
            //FileInfo fi = new FileInfo(fileName);

            //FileStream fs = fi.OpenRead();

            //NIFTY xy_F1,20091231,14:20,5205.70,5206.50,5204.00,5206.50,127350
            //20090915:09:56:14;260.55;260.4;260.7;2500;1250;0

            StreamReader sr = new StreamReader(fileName);
            StreamWriter sw = new StreamWriter(tickFileName);
            int tickNumber = 0;

            do
            {
                //tickNumber++;

                string tick = sr.ReadLine();

                string[] tickRecord = tick.Split(',');

                string tempStr = tickRecord[1].Insert(4, "-").Insert(7, "-");
                DateTime lastTradeTime = DateTime.Parse(tempStr);
                tempStr = tickRecord[2];

                lastTradeTime += TimeSpan.Parse(tempStr);

                double ltp = double.Parse(tickRecord[4]);
                double bid = double.Parse(tickRecord[5]);
                double offer = double.Parse(tickRecord[6]);

                // default 1000 offer and bid qty and volume traded as 0

                //Debug.Assert

                ////DerivativeQuoteInformation di = new DerivativeQuoteInformation
                ////(ltp, bid, offer, 1000, 1000);


                //////using (StreamWriter sw = new StreamWriter(fs))
                ////{
                ////    //DateTime Now = DateTime.Now;
                ////    string str = lastTradeTime.ToString("yyyyMMdd:HH:mm:ss") + ";" + di.LastTradedPriceDouble + ";" + di.BidPriceDouble +
                ////        ";" + di.OfferPriceDouble + ";" + di.BidQuantityInt + ";" + di.OfferQuantityInt + ";" + di.VolumeTradedInt;
                ////    sw.WriteLine(str);
                ////}

                

            } while (!sr.EndOfStream);// && tickNumber < 0);
        }
    }
}

